<template>
  <div class="box">
    <h2>子组件 add</h2>
    从vuex中获取的值:<label>{{ count }}</label>
    <br />
    <button @click="changeNum({type:'add',num:1})">值+1</button>
    <button @click="changeNum({type:'add',num:2})">值+2</button>
    <button @click="changeNum({type:'add',num:3})">值+3</button>
    <button @click="fn">等1秒后 加6</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex'

export default {
  name: 'AddItem',
  computed: {
    ...mapState(['count'])
  },
  methods: {
    ...mapMutations(['changeNum']),

    ...mapActions(['waitToAdd']),
    // mapActions和mapMutations类似，都是返回一个对象，内部定义了action方法
    fn () {
      // this.waitToAdd({ type: 'add', num: 6 })

      // 不用辅助函数的形式 action
      // this.$store.dispatch('action方法名', payload)
      this.$store.dispatch('waitToAdd', { type: 'add', num: 6 })
    }
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>
